package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.PermissionDTO;
import com.lastproject.ejb.entities.Permission;

/**
 * Session Bean implementation class Conversion
 */
@Stateless
@LocalBean
public class PermissionConversion {

	@PersistenceContext
	EntityManager em;

	/**
	 * Default constructor.
	 */
	public PermissionConversion() {
	}

	public PermissionDTO fromEntity(Permission p) {
		PermissionDTO perm = new PermissionDTO();
		perm.setId(p.getId());
		perm.setName(p.getName());
		return perm;
	}

	public Permission fromDTO(PermissionDTO p) {
		Permission perm = em.find(Permission.class, p.getId());
		if (perm == null) {
			perm = new Permission();
		}
		perm.setId(p.getId());
		perm.setName(p.getName());
		return perm;
	}

}
